package com.wangzhy.spider.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangzhy.common.bean.Result;
import com.wangzhy.common.util.DownloadUtil;
import com.wangzhy.spider.mapper.SpiderMapper;
import com.wangzhy.spider.service.ISpiderService;
import com.wangzhy.spider.vo.ImgVo;
import com.wangzhy.spider.vo.MerchandisePictureVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author ：wangzy
 * @date ：  2021/9/16 10:19
 * @description： 实现类
 */
@Service("spiderService")
@Slf4j
public class SpiderServiceImpl extends ServiceImpl<SpiderMapper, ImgVo> implements ISpiderService {
    @Autowired
    private SpiderMapper spiderMapper;

    @Override
    public List<ImgVo> selectPageLayoutImgList() {
        return spiderMapper.selectPageLayoutImgList(new HashMap<>(1));
    }

    @Override
    public List<ImgVo> selectAppHomeConfigImgList() {
        return spiderMapper.selectAppHomeConfigImgList();
    }

    /**
     * 下载商品图片 每次查询500 条数据
     * @return
     */
    @Override
    public Result spiderMerchandisePic() {
        int currentPage = 1;
        Page<MerchandisePictureVo> page = new Page(1,500);
        Page<MerchandisePictureVo> merchandisePicturePage = spiderMapper.getMerchandisePicList(page);
        log.info("一共 "+merchandisePicturePage.getTotal()+"条数据，共  "+merchandisePicturePage.getPages()+"页 ，每页 "+merchandisePicturePage.getSize()+"条数据！" );
       while (currentPage <= merchandisePicturePage.getPages()){
           log.info("当前页：=======>"+currentPage+",共 "+merchandisePicturePage.getPages()+"页");
           List<ImgVo> imgList = new ArrayList<>();
           List<MerchandisePictureVo> merchandiseList = merchandisePicturePage.getRecords();
           merchandiseList.forEach(x->{
               if(StrUtil.isNotBlank( x.getFrontPic())){
                   imgList.add(new ImgVo(x.getFrontPic(),null));
               }
               if(StrUtil.isNotBlank( x.getBackPic())){
                   imgList.add(new ImgVo(x.getBackPic(),null));
               }
               if(StrUtil.isNotBlank( x.getFortyfivePic())){
                   imgList.add(new ImgVo(x.getFortyfivePic(),null));
               }
               if(StrUtil.isNotBlank( x.getDisfrontPic())){
                   imgList.add(new ImgVo(x.getDisfrontPic(),null));
               }
               if(StrUtil.isNotBlank( x.getDownbasePic())){
                   imgList.add(new ImgVo(x.getDownbasePic(),null));
               }
               if(StrUtil.isNotBlank( x.getLeftsidePic())){
                   imgList.add(new ImgVo(x.getLeftsidePic(),null));
               }
               if(StrUtil.isNotBlank( x.getPackbackPic())){
                   imgList.add(new ImgVo(x.getPackbackPic(),null));
               }
               if(StrUtil.isNotBlank( x.getRightsidePic())){
                   imgList.add(new ImgVo(x.getRightsidePic(),null));
               }
               if(StrUtil.isNotBlank( x.getSpecificationPic())){
                   imgList.add(new ImgVo(x.getSpecificationPic(),null));
               }
           });
           if(imgList.size()>0){
               DownloadUtil.downloadImgs(imgList);
           }

           currentPage++;
           page.setCurrent(currentPage);
           if(currentPage<=merchandisePicturePage.getPages()){
               merchandisePicturePage = spiderMapper.getMerchandisePicList(page);
           }
       }


        return Result.success("商品图片下载成功");
    }


}
